getCode = function() {
  if (
    $("#mobile")
      .val()
      .trim().length === 0
  ) {
    app.alertError("请输入手机号");
    return;
  }

  // if (!(app.user.email.trim().length !== 0 && app.user["email:confirmed"])) {
  //   app.alertError("Bitte setzte/bestätige vorher deine Email.");
  //   return;
  // }
  var countdown = 180; //初始值
  function settime(e) {
    if (countdown == 0) {
      e.removeAttr("disabled");
      e.val("获取验证码");
      countdown = 180;
      return false;
    } else {
      e.attr("disabled", true);
      e.val(`重新发送(${countdown}s)`);
      countdown--;
    }
    setTimeout(function() {
      settime(e);
    }, 1000);
  }
  $.ajax("/api/plugins/aliyunsms-verify/getCode", {
    method: "post",
    data: {
      mobile: $("#mobile").val(),
      uid: $("#uid").val()
    }
  }).done(function(data) {
    if (data.error === true) {
      if (data.info) {
        app.alertError(data.info);
      } else {
        app.alertError("Client nicht gefunden!");
      }
    } else {
      app.alertSuccess("验证码已发送");
      settime($("#code-button"));
    }
  });
};

define("forum/account/aliyunsms", ["translator"], function(translator) {
  var Settings = {};

  Settings.init = function() {
    $('input[data-action="getCode"]').on("click", getCode);
    $('button[data-action="verify"]').on("click", Settings.verify);
  };

  Settings.verify = function() {
    if (
      $("#mobile")
        .val()
        .trim().length === 0
    ) {
      return;
    }
    if (
      $("#verify_code")
        .val()
        .trim().length === 0
    ) {
      return;
    }

    $.ajax("/api/plugins/aliyunsms-verify/verify", {
      method: "post",
      data: {
        mobile: $("#mobile").val(),
        code: $("#verify_code").val(),
        uid: $("#uid").val()
      }
    }).done(function(data) {
      if (data.error === true) {
        if (data.info) {
          app.alertError(data.info);
        } else {
          app.alertError("Client nicht gefunden!");
        }
      } else {
        app.alertSuccess(data.info);
        ajaxify.refresh();
      }
    });
  };

  Settings.getSms = function() {
    if (
      $("#mobile")
        .val()
        .trim().length === 0
    ) {
      return;
    }

    var countdown = 180; //初始值
    function settime(e) {
      if (countdown == 0) {
        e.removeAttr("disabled");
        e.val("获取验证码");
        countdown = 180;
        return false;
      } else {
        e.attr("disabled", true);
        e.val(`重新发送(${countdown}s)`);
        countdown--;
      }
      setTimeout(function() {
        settime(e);
      }, 1000);
    }
    $.ajax("/api/plugins/aliyunsms-verify/getCode", {
      method: "post",
      data: {
        mobile: $("#mobile").val(),
        uid: $("#uid").val()
      }
    }).done(function(data) {
      if (data.error === true) {
        if (data.info) {
          app.alertError(data.info);
        } else {
          app.alertError("Client nicht gefunden!");
        }
      } else {
        app.alertSuccess("验证码已发送");
        settime($("#code-button"));
      }
    });
  };

  Settings.beginSetup = function() {
    if (
      $("#tsid")
        .val()
        .trim().length === 0
    )
      return;

    if (!(app.user.email.trim().length !== 0 && app.user["email:confirmed"])) {
      app.alertError("Bitte setzte/bestätige vorher deine Email.");
      return;
    }

    $.ajax("/api/plugins/teamspeak-verify/checkUser", {
      method: "post",
      data: {
        tsid: $("#tsid").val()
      }
    }).done(function(data) {
      if (data.error === true) {
        found = false;
        if (data.info === "TS ID already verified") {
          app.alertError("Teamspeak ID wurde bereits verifiziert!");
        } else {
          app.alertError("Client nicht gefunden!");
        }
      } else {
        $.ajax("/api/plugins/teamspeak-verify/generate", {
          method: "post",
          data: {
            uid: $("#uid").val(),
            tsid: $("#tsid").val()
          }
        }).done(function(data) {
          if (data.error === false) {
            $("#codeCheck").modal("show");
          } else {
            app.alertError(data.info);
          }
        });
      }
    });
  };

  Settings.endSetup = function() {
    if (
      $("#tsid")
        .val()
        .trim().length === 0
    )
      return;
    $.ajax("/api/plugins/teamspeak-verify/check", {
      method: "post",
      data: {
        uid: $("#uid").val(),
        tsid: $("#tsid").val(),
        code: $("#randomcode").val()
      }
    }).done(function(data) {
      if (data.error === false) {
        $("#codeCheck").modal("hide");
        setTimeout(function() {
          ajaxify.refresh();
        }, 1000);
      } else {
        $("#randomcode").attr(
          "style",
          "border-radius: 5px; border:#FF0000 1px solid;"
        );
      }
    });
  };

  Settings.disassociate = function() {
    $.ajax("/api/plugins/aliyunsms-verify/disassociate", {
      method: "post",
      data: { uid: $("#uid").val() }
    }).done(function(data) {
      if (data.error === false) {
        ajaxify.refresh();
      } else {
        app.alertError(data.info);
      }
    });
  };

  return Settings;
});

$(function() {
  $(window).on("action:ajaxify.end", function(e, data) {
    if (data.url === "register") {
      $('input[data-action="getCode"]').on("click", getCode);
    }
  });
});
